import { Module } from 'vuex'
import { IRootState } from '@/store/type'
import { ISystemState } from './type'

import { getPageTableList, deletePageTableList, createPageTableList, editPageTableList } from '@/service/main/system/index'

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0
    }
  },
  getters: {
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`]
      }
    },
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`]
      }
    }
  },
  mutations: {
    changeUsersList(state, userList: any) {
      state.usersList = userList
    },
    changeUsersCount(state, userCount: number) {
      state.usersCount = userCount
    },
    changeMenuList(state, menuList: any) {
      state.menuList = menuList
    },
    changeMenuCount(state, menuCount: number) {
      state.menuCount = menuCount
    },
    changeGoodsList(state, goodsList: any) {
      state.goodsList = goodsList
    },
    changeGoodsCount(state, goodsCount: number) {
      state.goodsCount = goodsCount
    },
    changeRoleList(state, roleList: any) {
      state.roleList = roleList
    },
    changeRoleCount(state, roleCount: number) {
      state.roleCount = roleCount
    }
  },
  actions: {
    async getPageTableListAction({ commit }, payload: any) {
      // 1.获取pageUrl
      const { pageName } = payload
      const pageUrl = `/${pageName}/list`

      // 2.发送页面网络请求
      const tableResult = await getPageTableList(pageUrl, payload.queryInfo)

      // 3.触发mutations
      const { list, totalCount } = tableResult.data

      const changePageName = pageName.slice(0, 1).toUpperCase()
        + pageName.slice(1)

      commit(`change${changePageName}List`, list)
      commit(`change${changePageName}Count`, totalCount)
    },
    async deletePageTableListAction({ commit, dispatch }, payload: any) {
      const { pageName, id } = payload
      const pageUrl = `/${pageName}/${id}`

      await deletePageTableList(pageUrl)

      dispatch('getPageTableListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    async createPageTableListAction({ dispatch }, payload: any) {
      // 创建数据的请求
      const { pageName, queryInfo } = payload
      const pageUrl = `/${pageName}`

      // 发送网络请求
      await createPageTableList(pageUrl, queryInfo)

      dispatch('getPageTableListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },
    async editPageTableListAction({ dispatch }, payload: any) {
      console.log('payload', payload)
      // 创建数据的请求
      const { pageName, queryInfo, id } = payload
      const pageUrl = `/${pageName}/${id}`

      // 发送网络请求
      await editPageTableList(pageUrl, queryInfo)

      dispatch('getPageTableListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    }
  }
}

export default systemModule